package com.example.plane.GameView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import com.example.plane.R;

public class GameView extends SurfaceView implements SurfaceHolder.Callback,Runnable {
    public Canvas canvas ;
    public SurfaceHolder Holder;
    public Thread th;
    private boolean surfaceAvailable;
    private background  ba=new background();
    public GameView(Context context)
    {
        super(context);
        initView();
    }
    public void  initView()
    {
        Holder=getHolder();
        Holder.addCallback(this);


        //获取窗口管理器
//         bgwidth = wm.getDefaultDisplay().getWidth();
//         bgheight = wm.getDefaultDisplay().getHeight();
//        WindowManager windowManager = GameActivity.instance.getWindowManager();
//        Display display = windowManager.getDefaultDisplay();
//
//        //Andorid.util 包下的DisplayMetrics 类提供了一种关于显示的通用信息，如显示大小，分辨率和字体
//        DisplayMetrics metrics = new DisplayMetrics();
        //将当前窗口中的一些信息放在DisplayMetrics里面
//        display.getMetrics(metrics);

//        for (int i = 0; i < fristpass; i++) {
//          bitmap[i] = BitmapFactory.decodeResource(getResources(), R.drawable.qj0);
//            bgwidth[i] = bitmap[i].getWidth();
//            bgheight[i] = bitmap[i].getHeight();
//            scaleWidth[i] = ((float) wm.getDefaultDisplay().getWidth())/ bgwidth[i];//计算宽度的缩放比例
//            scaleHeight[i] = ((float) wm.getDefaultDisplay().getHeight())/ bgheight[i];//计算高度的缩放比例
//            Matrix matrix = new Matrix();//矩阵类用于图像坐标的转换，以及旋转，缩放等特殊操作。
//            matrix.postScale(scaleWidth[i], scaleHeight[i]);//设置缩放
//            //如果将实例化在外面即不释放内存，该对象会一直缩放
//            bitmap[i] = Bitmap.createBitmap(bitmap[i], 0, 0, bgwidth[i], bgheight[i], matrix, false);//创建一个缩放后的图片
//        }
//        currX[0]=0;
//        currY[0]=0;
//        currX[1]=0;;
//        currY[1]=-bitmap[1].getHeight();
        ba.initView();
        setFocusable(true);
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);

    }

    public void Draw()
    {

//
//        //用 画笔 在 画布 的 某个位置 去画 图片
//        paint();
      canvas = Holder.lockCanvas();//获取该view下的画布
        ba.doDraw(canvas);
      Holder.unlockCanvasAndPost(canvas);

    }
    public void surfaceCreated(SurfaceHolder holder) {
//        screenW = this.getWidth();
//        screenH = this.getHeight();
//        initGame();
//        flag = true;
        // 实例线程
        surfaceAvailable = true;
        if(null == th){
            th = new Thread(this);
        }
        // 启动线程
        th.start();
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        surfaceAvailable = false;
    }
    public void logic()
    {
    ba.bglogic();
    }
    public void run()
    {
        while(surfaceAvailable) {
            Draw();
            logic();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}


